package org.example.musicproject.dao.impl;

import org.example.musicproject.dao.SingerDao;
import org.example.musicproject.entity.Singer;
import org.example.musicproject.util.DButil;

import java.sql.*;
import java.util.Collections;
import java.util.List;

/**
 * 创建人:黄庭萍
 * 创建时间: 2024/12/10 20:44
 * 创建类型: SingerDaoImpl
 */
public class SingerDaoImpl implements SingerDao {

    @Override
    public List<Singer> all() {
        String sql = "select id AS siid, name AS siname, sex, pic AS sipic, birth,location,introduction AS siintroduction from singer";
        System.out.println("=====查询所有歌手信息============");
        System.out.println("sql="+sql);
        System.out.println("================================");
        return DButil.list(sql, Singer.class);
    }

    @Override
    public Singer byId(int id) {
        String sql = "select id AS siid, name AS siname, sex, pic AS sipic, birth,location,introduction AS siintroduction from singer where id=?";
        System.out.println("=====根据id查询歌手信息============");
        System.out.println("sql="+sql);
        System.out.println("id="+id);
        System.out.println("================================");
        return DButil.one(sql, Singer.class, id);
    }

    @Override
    public long add(Connection conn, Singer singer) {
        String sql="insert into singer(name,sex,pic,birth,location,introduction) values(?,?,?,?,?,?)";
        long id = 0l;
        try (PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)){
            ps.setString(1, singer.getSiname());
            ps.setString(2,singer.getSex());
            ps.setString(3, singer.getSipic());
            ps.setDate(4,singer.getBirth());
            ps.setString(5,singer.getLocation());
            ps.setString(6,singer.getSiintroduction());

            int rows = ps.executeUpdate();
            if(rows==0){
                throw new SQLException("学期添加失败！");
            }

            try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
                if (generatedKeys.next()) {
                    id = generatedKeys.getLong(1);
                    System.out.println("生成歌手密码: " + id);
                } else {
                    throw new SQLException("歌手添加失败！没有生成歌手ID编号.");
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("歌手添加失败！原因: " + e.getMessage());
        }

        System.out.println("=====根据id添加歌手信息========");
        System.out.println("sql=" + sql);
        System.out.println("conn=" + conn);
        System.out.println("singer = " + singer);
        System.out.println("========================");
        return id;
    }

    @Override
    public int update(Singer singer) {
        String sql = "update singer set name=?,sex=?,pic=?,birth=?,location=?,introduction=? where id=?";
        System.out.println("=====根据id修改歌手信息========");
        System.out.println("sql=" + sql);
        System.out.println("singer = " + singer);
        System.out.println("========================");
        return DButil.update(sql, singer.getSiname(),singer.getSex(),singer.getSipic(),singer.getBirth(),
                singer.getLocation(),singer.getSiintroduction(),singer.getSiid());
    }

    @Override
    public List<Singer> bykey(String key) {
        if (key == null || key.trim().isEmpty()) {
            throw new IllegalArgumentException("关键字不能为空");
        }

        String sql = "SELECT id AS siid, name AS siname, sex, pic AS sipic, birth, location, introduction AS siintroduction FROM singer WHERE name LIKE ?";
        System.out.println("=====根据关键字查询歌手信息========");
        System.out.println("sql=" + sql);
        System.out.println("key=" + key);
        System.out.println("========================");

        // 使用通配符 % 包裹关键字，以便进行模糊匹配
        List<Singer> singers = DButil.list(sql, Singer.class, "%" + key + "%");

        return singers != null ? singers : Collections.emptyList();
    }
}
